package com.yida.controller;

import com.yida.bean.comment;
import com.yida.bean.tag;
import com.yida.service.serverblog;
import com.yida.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;



@Controller
public class blog {

	@Autowired
	public serverblog service;
	
	@RequestMapping("/blog")
	public String index(Model mode,Integer id) {
	
		//按时间排序的博
	    List<com.yida.bean.blog> blogxin= service.selectblog(1);
	    //增加阅读次数
	    service.updateblogsumservice(id);
	    //博客的详情
	    com.yida.bean.blog   blogxq = service.selectBlogservice(id);
	    
	    String content =  blogxq.getContent();
	    if (content!=null) {
	    	blogxq.setContent( MarkdownUtils.markdownToHtmlExtensions(content));
		}

	    //查询标签
	    List<tag> tags = service.selecttagservice();
	    //放入评论
	    List<comment> listcomment =   service.listCommentByBlogId(id);
		mode.addAttribute("comments", listcomment);
		
	   mode.addAttribute("blogxq", blogxq);
	   mode.addAttribute("blogxin", blogxin);
	   mode.addAttribute("tags", tags);
	   
		return "blog";
	}
	
	
	@RequestMapping("/blog/comments")
	public String comments(Model mode,Integer id) {
		List<comment> listcomment =   service.listCommentByBlogId(id);
		mode.addAttribute("comments", listcomment);
		return "blog :: commentList";
	}
	

	@RequestMapping("/blog/post")
	public String post(comment comment,HttpSession session,Integer blogxqid,String email) {
		int replyid = comment.getReplyid();
		int blogid = comment.getBlogId();
		if (replyid != -1) {
			comment.setBlogId(null);
		}else {
			comment.setReplyid(null);
		}	
		//设置时间
		Date date = new Date();
		comment.setConmmentDate(new Timestamp(date.getTime()));
        //插入
        Integer pdInteger = service.saveConmment(comment,session,1,blogxqid,email);
		return "redirect:/blog/comments?id="+blogid;
	}
	
	
	
	
	
}
